﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace LGCourt.Logic.Common
{
    public class DepartmentHandler
    {
        private static XmlDocument _xmlDoc;
        public DepartmentHandler()
        {
            if (_xmlDoc == null)
            {
                _xmlDoc = new XmlDocument();
                _xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"Department.xml");
            }
        }        

        public List<Department> GetDepartmentList()
        {
            List<Department> result = new List<Department>();
            try
            {
                XmlNodeList xmlNodeListBase = _xmlDoc.SelectNodes(@"Departments/Department");

                foreach (XmlNode node in xmlNodeListBase)
                {
                    Department info = new Department();
                    info.Name = node.Attributes["Name"].Value;
                    info.Code = node.Attributes["Code"].Value;
                    info.RowIndex = node.Attributes["RowIndex"] == null ? 1 : int.Parse(node.Attributes["RowIndex"].Value);
                    result.Add(info);
                }
            }
            catch (Exception e)
            {
                throw new Exception("不能读取配置文件：" + e.Message);
            }
            return result;
        }

        public Department GetDepartmentInfo(string Code)
        {
            Department result = new Department();
            try
            {
                XmlNode xmlNode = _xmlDoc.SelectSingleNode(@"Departments/Department[(@Code='" + Code + "')]");

                if (xmlNode == null)
                    return result;
                result.Name = xmlNode.Attributes["Name"].Value;
                result.Code = xmlNode.Attributes["Code"].Value;
                result.RowIndex = xmlNode.Attributes["RowIndex"] == null ? 1 : int.Parse(xmlNode.Attributes["RowIndex"].Value);
                List<DeptItem> list=new List<DeptItem>();
                foreach (XmlNode node in xmlNode.ChildNodes)
                {
                    DeptItem item = new DeptItem();
                    item.nCode = node.Attributes["nCode"] == null ? "" : node.Attributes["nCode"].Value;
                    item.nClass = node.Attributes["nClass"] == null ? "" : node.Attributes["nClass"].Value;
                    item.nPosition = node.Attributes["nPosition"] == null ? "" : node.Attributes["nPosition"].Value;
                    item.nName = node.Attributes["nName"] == null ? "" : node.Attributes["nName"].Value;
                    item.nTitle = node.Attributes["nTitle"] == null ? "" : node.Attributes["nTitle"].Value;
                    item.nDescription = node.Attributes["nDescription"] == null ? "" : node.Attributes["nDescription"].Value;
                    item.nUrl = node.Attributes["nUrl"] == null ? "" : node.Attributes["nUrl"].Value;                   
                    list.Add(item);
                    
                }
                result.DeptItemList=list;
            }
            catch (Exception e)
            {
                throw new Exception("不能读取配置文件：" + e.Message);
            }
            return result;
        }
    }

    public class Department
    {
        public string Name { get; set; }
        public string Code { get; set; }
        public int RowIndex { get; set; }
        public List<DeptItem> DeptItemList{ get; set; }
    }

    public class DeptItem
    {
        public string nCode { get; set; }
        public string nClass { get; set; }
        public string nPosition { get; set; }
        public string nName { get; set; }
        public string nTitle { get; set; }
        public string nDescription { get; set; }
        public string nUrl { get; set; }
    }


}
